package ac.snu.street;

 
import java.util.ArrayList;
 
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
 
import com.google.android.maps.ItemizedOverlay;
 
public class MyItemizedOverlay extends ItemizedOverlay {
 
    private ArrayList<OverlayPostItem> mOverlays = new ArrayList<OverlayPostItem>();
    private Context mContext;
    
    // constructor
    public MyItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }
      
    public void addOverlay(OverlayPostItem overlay) {
        mOverlays.add(overlay);
        populate();
    }
      
    @Override
    protected OverlayPostItem createItem(int i) {
      return mOverlays.get(i);
    }
  
    @Override
    public int size() {
        return mOverlays.size();
    }
      
    @Override
    protected boolean onTap(int index) {
      final OverlayPostItem item = mOverlays.get(index);
      final LinearLayout linear = (LinearLayout) View.inflate(mContext,R.layout.reply, null);
      //final LinearLayout linear = (LinearLayout) View.inflate(mContext,R.layout.textedit, null);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      if(item.getId() != 0)
      {
    	  dialog.setPositiveButton("답변달기", new DialogInterface.OnClickListener() {
    	  @Override
    	  public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
    		  
    		  AlertDialog.Builder dialogReply = new AlertDialog.Builder(mContext);
    	      dialogReply.setTitle("답변");
    	      dialogReply.setView(linear);
    	      if(item.getId() != 0)
    	      {
    	    	  dialogReply.setPositiveButton("답변", new DialogInterface.OnClickListener() {
    	    	  @Override
    	    	  public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub
    	    		  
    	    		    EditText txt = (EditText)linear.findViewById(R.id.replyTxt);
    					String txtContent = txt.getText().toString();
    					
    					StreetPostActivity.remoteDB.update(item.getId(), item.getSnippet() + "\n  Re : " + txtContent);
    	    	  } 
    	    	  });  
    	   	  dialogReply.setNegativeButton("취소",null);
    	      }
    	      dialogReply.show();
    	         		    
    	  } 
    	  });  
    	  
      dialog.setNegativeButton("닫기",null);
      }
      dialog.show();
      return true;
    }  
  
    //public void update(int id, StreetNote note) {
}  
    
